package com.ruoyi.customer.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.PrimaryKeyIdUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.customer.mapper.ShippingAddressMapper;
import com.ruoyi.system.api.domain.ShippingAddress;
import com.ruoyi.customer.service.IShippingAddressService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 收货地址Service业务层处理
 * 
 * @author luogongquan
 * @date 2024-09-10
 */
@Service
public class ShippingAddressServiceImpl implements IShippingAddressService 
{
    @Autowired
    private ShippingAddressMapper shippingAddressMapper;

    /**
     * 查询收货地址
     * 
     * @param id 收货地址主键
     * @return 收货地址
     */
    @Override
    public ShippingAddress selectShippingAddressById(Long id)
    {
        return shippingAddressMapper.selectShippingAddressById(id);
    }

    /**
     * 查询收货地址列表
     * 
     * @param shippingAddress 收货地址
     * @return 收货地址
     */
    @Override
    public List<ShippingAddress> selectShippingAddressList(ShippingAddress shippingAddress)
    {
        return shippingAddressMapper.selectShippingAddressList(shippingAddress);
    }

    /**
     * 新增收货地址
     * 
     * @param shippingAddress 收货地址
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertShippingAddress(ShippingAddress shippingAddress)
    {
        shippingAddress.setId(PrimaryKeyIdUtils.getSysIdWorker().nextId());
        if(shippingAddress.getDefaultFlag().equals("1")){
            //将其他地址改为非默认
            shippingAddressMapper.updateDefaultFlagByCustomerId("2",shippingAddress.getCustomerId());
        }
        return shippingAddressMapper.insertShippingAddress(shippingAddress);
    }

    /**
     * 修改收货地址
     * 
     * @param shippingAddress 收货地址
     * @return 结果
     */
    @Override
    public int updateShippingAddress(ShippingAddress shippingAddress)
    {
        if("1".equals(shippingAddress.getDefaultFlag())){
            //将其他地址改为非默认
            shippingAddressMapper.updateDefaultFlagByCustomerId("2",shippingAddress.getCustomerId());
        }
        return shippingAddressMapper.updateShippingAddress(shippingAddress);
    }

    /**
     * 批量删除收货地址
     * 
     * @param ids 需要删除的收货地址主键
     * @return 结果
     */
    @Override
    public int deleteShippingAddressByIds(Long[] ids)
    {
        return shippingAddressMapper.deleteShippingAddressByIds(ids);
    }

    /**
     * 删除收货地址信息
     * 
     * @param id 收货地址主键
     * @return 结果
     */
    @Override
    public int deleteShippingAddressById(Long id)
    {
        return shippingAddressMapper.deleteShippingAddressById(id);
    }

    @Override
    public ShippingAddress getDefaultAddress() {
        return shippingAddressMapper.getDefaultAddress(SecurityUtils.getUserId());
    }
}
